﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication2
{
    public partial class _Default : Page
    {
        private DataTable gvDataTable = new DataTable();

        private DataTable Data {
            get{
                var sql = "select * from Articles";
                var dt = DbHelper.GetDataTable(sql);
                return dt;

            }
        }
        private DataTable DataOther {
            get {
                if (gvDataTable.Rows.Count > 0)
                {
                    return gvDataTable;
                }
                else {
                    var sql = "select * from Articles";
                    var dt = DbHelper.GetDataTable(sql);
                    gvDataTable = dt;
                    return dt;
                 
                }
            
            }
        
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack) {
                FillData();
            }
        }

        private void FillData()
        {
            gvl.DataSource = this.Data;
            gvl.DataBind();
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            var dt = this.Data.Copy();
            dt.Rows.Add(dt.NewRow());

            gvDataTable = dt;
            gvl.DataSource = gvDataTable;
            gvl.DataBind();

            gvl.EditIndex = dt.Rows.Count - 1;
            gvl.DataSource = gvDataTable;
            gvl.DataBind();
        }

        protected void gvl_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gvl.EditIndex = -1;
            FillData();
        }

        protected void gvl_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            var id = GetCellValues(e.RowIndex, 0);
            var sql = string.Format("delete Articles where Id={0}", id);
            DbHelper.AddOrDeleteOrUpdate(sql);
            gvl.EditIndex = -1;
            FillData();
        }

        private string GetCellValues(int rowIndex, int colIndex)
        {
            var cell = gvl.Rows[rowIndex].Cells[colIndex];
            var k = cell.Controls.Count > 0 ? ((TextBox)cell.Controls[0]).Text : cell.Text;
            return k;
        }

        protected void gvl_RowEditing(object sender, GridViewEditEventArgs e)
        {
            gvl.EditIndex = e.NewEditIndex;
            FillData();


        }

        protected void gvl_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            var idString = GetCellValues(e.RowIndex, 0);
            var isOK = int.TryParse(idString, out int id);

            var title = GetCellValues(e.RowIndex, 1);
            var content = GetCellValues(e.RowIndex, 2);
            var author = GetCellValues(e.RowIndex, 3);
            var remarks = GetCellValues(e.RowIndex, 4);
            gvl.EditIndex = -1;
            FillData();

            if (isOK)
            {

                var sql1 = string.Format("update Articles set Title='{0}',Content='{1}',Author='{2}',Remarks='{3}' where Id='{4}' ", title, content, author, remarks, id);
                DbHelper.AddOrDeleteOrUpdate(sql1);
            }
            else {
                var sql2 = string.Format("insert into Articles(Title,Content,Author,Remarks)Values({0},{1},{2},{3},{4}) ", title, content, author, remarks, id);
                DbHelper.AddOrDeleteOrUpdate(sql2);
            }
            gvl.EditIndex = -1;
            FillData();
        }
    }
}